#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;
using ll = long long;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 1e5 + 10;

int n, a[N], K, tim[N];

void solve() {
	n = in; K = in; rep(i, 0, n - 1) a[i] = in;
	rep(i, 0, n - 1) tim[i] = 0;
	queue < pii > q; 
	rep(i, 0, n - 1) {
		if(a[i] > 0) q.push({ i, 1 }), tim[i] = 1;
	}
	if(!q.size()) return puts("0"), void();
	while(q.size()) {
		int x = q.front().fi, t = q.front().se; q.pop();
		int l = (x - 1 + n) % n, r = (x + 1) % n;
		if(!tim[l]) tim[l] = t + 1, q.push({ l, t + 1 });
		if(!tim[r]) tim[r] = t + 1, q.push({ r, t + 1 });
	}
	ll ans = 0;
	rep(i, 0, n - 1) {
		int l = (i - 1 + n) % n, r = (i + 1) % n;
		int res = max(K - tim[l] + 1, 0) + max(K - tim[r] + 1, 0);
		ans += a[i] + res;
	}
	cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    int t = in; while(t--) solve();
}
